<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>equal_range</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Algorithms</a> &gt; <a href=
    "equal_range.html">equal_range</a>
  </div>

  <div class="name-format">
    equal_range
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;algorithm&gt;
  pair&lt;iterator,iterator&gt; equal_range( iterator first, iterator last, const <a href="../containers.html">TYPE</a>&amp; val );
  pair&lt;iterator,iterator&gt; equal_range( iterator first, iterator last, const <a href="../containers.html">TYPE</a>&amp; val, CompFn comp );
</pre>

  <p>The equal_range() function returns the range of elements between
  <em>first</em> and <em>last</em> that are equal to <em>val</em>. This
  function assumes that the elements between <em>first</em> and
  <em>last</em> are in order according to <em>comp</em>, if it is
  specified, or the &lt; operator otherwise.</p>

  <p>equal_range() can be thought of as a combination of the <a href=
  "lower_bound.html">lower_bound</a>() and `upper_bound1`() functions,
  since the first of the pair of iterators that it returns is what
  <a href="lower_bound.html">lower_bound</a>() returns and the second
  iterator in the pair is what `upper_bound1`() returns.</p>

  <p>For example, the following code uses equal_range() to determine
  all of the possible places that the number 8 can be inserted into an
  ordered vector of integers such that the existing ordering is
  preserved:</p>
  <pre class="example-code">
 vector&lt;int&gt; nums;
 nums.push_back( -242 );
 nums.push_back( -1 );
 nums.push_back( 0 );
 nums.push_back( 5 );
 nums.push_back( 8 );
 nums.push_back( 8 );
 nums.push_back( 11 );          

 pair&lt;vector&lt;int&gt;::iterator, vector&lt;int&gt;::iterator&gt; result;
 int new_val = 8;               

 result = equal_range( nums.begin(), nums.end(), new_val );             

 cout &lt;&lt; &quot;The first place that &quot; &lt;&lt; new_val &lt;&lt; &quot; could be inserted is before &quot;
      &lt;&lt; *result.first &lt;&lt; &quot;, and the last place that it could be inserted is before &quot;
      &lt;&lt; *result.second &lt;&lt; endl;            
</pre>

  <p>The above code produces the following output:</p>
  <pre class="example-code">
 The first place that 8 could be inserted is before 8,
 and the last place that it could be inserted is before 11              
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="binary_search.html">binary_search</a><br>
    <a href="lower_bound.html">lower_bound</a><br>
    <a href="upper_bound.html">upper_bound</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
